<html>
<head>
    <script src="../OLLoader.js"></script>
    <script type="text/javascript">

    function test_write_WPSExecute_WCS(t) {
        t.plan(1);
        var expected = '<?xml version="1.0" encoding="UTF-8"?>' +
'<wps:Execute version="1.0.0" service="WPS" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.opengis.net/wps/1.0.0" xmlns:wfs="http://www.opengis.net/wfs" xmlns:wps="http://www.opengis.net/wps/1.0.0" xmlns:ows="http://www.opengis.net/ows/1.1" xmlns:gml="http://www.opengis.net/gml" xmlns:ogc="http://www.opengis.net/ogc" xmlns:wcs="http://www.opengis.net/wcs" xmlns:xlink="http://www.w3.org/1999/xlink" xsi:schemaLocation="http://www.opengis.net/wps/1.0.0 http://schemas.opengis.net/wps/1.0.0/wpsAll.xsd">' +
'  <ows:Identifier>gs:GeorectifyCoverage</ows:Identifier>' +
'  <wps:DataInputs>' +
'    <wps:Input>' +
'      <ows:Identifier>data</ows:Identifier>' +
'      <wps:Reference mimeType="image/tiff" xlink:href="http://geoserver/wcs" method="POST">' +
'        <wps:Body>' +
'          <wcs:GetCoverage service="WCS" version="1.1.2">' +
'            <ows:Identifier>topp:asbuilt</ows:Identifier>' +
'            <wcs:DomainSubset>' +
'              <ows:BoundingBox crs="http://www.opengis.net/gml/srs/epsg.xml#404000">' +
'                <ows:LowerCorner>0 -7070</ows:LowerCorner>' +
'                <ows:UpperCorner>10647 1</ows:UpperCorner>' +
'              </ows:BoundingBox>' +
'            </wcs:DomainSubset>' +
'            <wcs:Output format="image/tiff"/>' +
'          </wcs:GetCoverage>' +
'        </wps:Body>' +
'      </wps:Reference>' +
'    </wps:Input>' +
'    <wps:Input>' +
'      <ows:Identifier>gcp</ows:Identifier>' +
'      <wps:Data>' +
'        <wps:LiteralData>[[[2721, 3263], [-122.472109, 37.73106003]], [[4163, 3285], [-122.4693417, 37.729929851]], [[5773, 4046], [-122.466702461, 37.7271906]], [[8885, 4187], [-122.462333, 37.725167]]]</wps:LiteralData>' +
'      </wps:Data>' +
'    </wps:Input>' +
'    <wps:Input>' +
'      <ows:Identifier>targetCRS</ows:Identifier>' +
'      <wps:Data>' +
'        <wps:LiteralData>EPSG:4326</wps:LiteralData>' +
'      </wps:Data>' +
'    </wps:Input>' +
'    <wps:Input>' +
'      <ows:Identifier>transparent</ows:Identifier>' +
'      <wps:Data>' +
'        <wps:LiteralData>true</wps:LiteralData>' +
'      </wps:Data>' +
'    </wps:Input>' +
'  </wps:DataInputs>' +
'  <wps:ResponseForm>' +
'    <wps:RawDataOutput mimeType="image/tiff">' +
'      <ows:Identifier>result</ows:Identifier>' +
'    </wps:RawDataOutput>' +
'  </wps:ResponseForm>' +
'</wps:Execute>';

        var format = new OpenLayers.Format.WPSExecute();
        var result = format.write({
            identifier: "gs:GeorectifyCoverage", 
            dataInputs: [{
                identifier: 'data',
                reference: {
                    mimeType: "image/tiff", 
                    href: "http://geoserver/wcs", 
                    method: "POST",
                    body: {
                        wcs: {
                            identifier: 'topp:asbuilt',
                            version: '1.1.2',
                            domainSubset: {
                                boundingBox: {
                                    projection: 'http://www.opengis.net/gml/srs/epsg.xml#404000',
                                    bounds: new OpenLayers.Bounds(0.0, -7070.0, 10647.0, 1.0)
                                }
                            },
                            output: {format: 'image/tiff'}
                        }
                    }
                }
            }, {
                identifier: 'gcp',
                data: {
                    literalData: {
                        value: '[[[2721, 3263], [-122.472109, 37.73106003]], [[4163, 3285], [-122.4693417, 37.729929851]], [[5773, 4046], [-122.466702461, 37.7271906]], [[8885, 4187], [-122.462333, 37.725167]]]'
                    }
                }
            }, {
                identifier: 'targetCRS',
                data: {
                    literalData: {
                        value: 'EPSG:4326'
                    }
                }
            }, {
                identifier: 'transparent',
                data: {
                    literalData: {
                        value: 'true'
                    }
                }
            }],
            responseForm: {
                rawDataOutput: {
                    mimeType: "image/tiff",
                    identifier: "result"
                }
            }
        });
        t.xml_eq(result, expected, "WPS Execute with embedded WCS GetCoverage written out correctly");

    }

    function test_write_WPSExecute(t) {
        t.plan(1);
        var expected = '<?xml version="1.0" encoding="UTF-8"?>' +
'<wps:Execute version="1.0.0" service="WPS" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' +
'    xmlns="http://www.opengis.net/wps/1.0.0" xmlns:wfs="http://www.opengis.net/wfs"' +
'    xmlns:wps="http://www.opengis.net/wps/1.0.0" xmlns:ows="http://www.opengis.net/ows/1.1"' +
'    xmlns:gml="http://www.opengis.net/gml" xmlns:ogc="http://www.opengis.net/ogc"' +
'    xmlns:wcs="http://www.opengis.net/wcs/1.1.1" xmlns:xlink="http://www.w3.org/1999/xlink"' +
'    xsi:schemaLocation="http://www.opengis.net/wps/1.0.0 http://schemas.opengis.net/wps/1.0.0/wpsAll.xsd">' +
'    <ows:Identifier>JTS:area</ows:Identifier>' +
'    <wps:DataInputs>' +
'        <wps:Input>' +
'            <ows:Identifier>geom</ows:Identifier>' +
'            <wps:Reference mimeType="text/xml; subtype=gml/3.1.1" xlink:href="http://geoserver/wps"' +
'                method="POST">' +
'                <wps:Body>' +
'                    <wps:Execute service="WPS" version="1.0.0">' +
'                        <ows:Identifier>gs:CollectGeometries</ows:Identifier>' +
'                        <wps:DataInputs>' +
'                            <wps:Input>' +
'                                <ows:Identifier>features</ows:Identifier>' +
'                                <wps:Reference mimeType="text/xml; subtype=wfs-collection/1.0"' +
'                                    xlink:href="http://geoserver/wfs" method="POST">' +
'                                    <wps:Body>' +
'                                        <wfs:GetFeature service="WFS" version="1.0.0"' +
'                                            outputFormat="GML2">' +
'                                            <wfs:Query typeName="sf:archsites"/>' +
'                                        </wfs:GetFeature>' +
'                                    </wps:Body>' +
'                                </wps:Reference>' +
'                            </wps:Input>' +
'                        </wps:DataInputs>' +
'                        <wps:ResponseForm>' +
'                            <wps:RawDataOutput mimeType="text/xml; subtype=gml/3.1.1">' +
'                                <ows:Identifier>result</ows:Identifier>' +
'                            </wps:RawDataOutput>' +
'                        </wps:ResponseForm>' +
'                    </wps:Execute>' +
'                </wps:Body>' +
'            </wps:Reference>' +
'        </wps:Input>' +
'    </wps:DataInputs>' +
'    <wps:ResponseForm>' +
'        <wps:RawDataOutput>' +
'            <ows:Identifier>result</ows:Identifier>' +
'        </wps:RawDataOutput>' +
'    </wps:ResponseForm>' +
'</wps:Execute>';

        var format = new OpenLayers.Format.WPSExecute();
        var result = format.write({
            identifier: "JTS:area", 
            dataInputs: [{
                identifier: 'geom',
                reference: {
                    mimeType: "text/xml; subtype=gml/3.1.1", 
                    href: "http://geoserver/wps", 
                    method: "POST",
                    body: {
                        identifier: "gs:CollectGeometries",
                        dataInputs: [{
                             identifier: 'features',
                             reference: {
                                 mimeType: "text/xml; subtype=wfs-collection/1.0",
                                 href: "http://geoserver/wfs",
                                 method: "POST",
                                 body: {
                                     wfs: {
                                         version: "1.0.0",
                                         outputFormat: "GML2",
                                         featureType: "sf:archsites"
                                     }
                                 }
                             }
                         }],
                         responseForm: {
                             rawDataOutput: {
                                 mimeType: "text/xml; subtype=gml/3.1.1",
                                 identifier: "result"
                             }
                         }
                    }
                }
            }],
            responseForm: {
                rawDataOutput: {
                    identifier: "result"
                }
            }
        });
        t.xml_eq(result, expected, "WPS Execute written out correctly");
    }

    function test_write_raw_data_output(t) {
        t.plan(1);
        // example request taken from: http://geoprocessing.info/wpsdoc/1x0ExecutePOST
        var expected = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' +
'<wps:Execute service="WPS" version="1.0.0" xmlns:wps="http://www.opengis.net/wps/1.0.0" xmlns:ows="http://www.opengis.net/ows/1.1" ' +
'xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/wps/1.0.0 http://schemas.opengis.net/wps/1.0.0/wpsAll.xsd">' +
'	<ows:Identifier>Buffer</ows:Identifier>' +
'	<wps:DataInputs>' +
'		<wps:Input>' +
'			<ows:Identifier>InputPolygon</ows:Identifier>' +
'			<ows:Title>Playground area</ows:Title>' +
'			<wps:Reference xlink:href="http://foo.bar/some_WFS_request.xml"/>' +
'		</wps:Input>' +
'		<wps:Input>' +
'			<ows:Identifier>BufferDistance</ows:Identifier>' +
'			<ows:Title>Distance which people will walk to get to a playground.</ows:Title>' +
'			<wps:Data>' +
'				<wps:LiteralData>400</wps:LiteralData>' +
'			</wps:Data>' +
'		</wps:Input>' +
'	</wps:DataInputs>' +
'	<wps:ResponseForm>' +
'		<wps:RawDataOutput>' +
'			<ows:Identifier>BufferedPolygon</ows:Identifier>' +
'		</wps:RawDataOutput>' +
'	</wps:ResponseForm>' +
'</wps:Execute>';

        var format = new OpenLayers.Format.WPSExecute();
        var result = format.write({
            identifier: "Buffer", 
            dataInputs: [{
                identifier: 'InputPolygon',
                title: 'Playground area',
                reference: {
                    href: 'http://foo.bar/some_WFS_request.xml'
                }
            }, {
                identifier: 'BufferDistance',
                title: 'Distance which people will walk to get to a playground.',
                data: {
                    literalData: {
                        value: 400
                    }
                }
            }],
            responseForm: {
                rawDataOutput: {
                    identifier: "BufferedPolygon"
                }
            }
        });
        t.xml_eq(result, expected, "WPS Execute written out correctly");
    }

    function test_write_request_responseDoc_defaultFormat(t) {
        t.plan(1);
        // taken from http://geoprocessing.info/schemas/wps/1.0/examples/51_wpsExecute_request_ResponseDocument.xml
        var expected = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' +
'<wps:Execute service="WPS" version="1.0.0" xmlns:wps="http://www.opengis.net/wps/1.0.0" xmlns:ows="http://www.opengis.net/ows/1.1" ' +
'xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/wps/1.0.0 http://schemas.opengis.net/wps/1.0.0/wpsAll.xsd">' +
'	<ows:Identifier>Buffer</ows:Identifier>' +
'	<wps:DataInputs>' +
'		<wps:Input>' +
'			<ows:Identifier>InputPolygon</ows:Identifier>' +
'			<ows:Title>Playground area</ows:Title>' +
'			<wps:Reference xlink:href="http://foo.bar/some_WFS_request.xml"/>' +
'		</wps:Input>' +
'		<wps:Input>' +
'			<ows:Identifier>BufferDistance</ows:Identifier>' +
'			<ows:Title>Distance which people will walk to get to a playground.</ows:Title>' +
'			<wps:Data>' +
'				<wps:LiteralData>400</wps:LiteralData>' +
'			</wps:Data>' +
'		</wps:Input>' +
'	</wps:DataInputs>' +
'	<wps:ResponseForm>' +
'		<wps:ResponseDocument storeExecuteResponse="true">' +
'			<wps:Output asReference="true">' +
'				<ows:Identifier>BufferedPolygon</ows:Identifier>' +
'				<ows:Title>Area serviced by playground.</ows:Title>' +
'				<ows:Abstract>Area within which most users of this playground will live.</ows:Abstract>' +
'			</wps:Output>' +
'		</wps:ResponseDocument>' +
'	</wps:ResponseForm>' +
'</wps:Execute>';

        var format = new OpenLayers.Format.WPSExecute();
        var result = format.write({
            identifier: "Buffer", 
            dataInputs: [{
                identifier: 'InputPolygon',
                title: 'Playground area',
                reference: {
                    href: 'http://foo.bar/some_WFS_request.xml'
                }
            }, {
                identifier: 'BufferDistance',
                title: 'Distance which people will walk to get to a playground.',
                data: {
                    literalData: {
                        value: 400
                    }
                }                    
            }],
            responseForm: {
                responseDocument: {
                    storeExecuteResponse: true,
                    outputs: [{
                        asReference: true,
                        identifier: 'BufferedPolygon',
                        title: 'Area serviced by playground.',
                        'abstract': 'Area within which most users of this playground will live.'
                    }]
                }
            }
        });
        t.xml_eq(result, expected, "WPS Execute written out correctly");
    }

    function test_write_request_responseDoc_specifiedFormat(t) {
        t.plan(1);
        // taken from http://geoprocessing.info/schemas/wps/1.0/examples/52_wpsExecute_request_ResponseDocument.xml
        var expected = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' +
'<wps:Execute service="WPS" version="1.0.0" xmlns:wps="http://www.opengis.net/wps/1.0.0" xmlns:ows="http://www.opengis.net/ows/1.1" ' +
'xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/wps/1.0.0 http://schemas.opengis.net/wps/1.0.0/wpsAll.xsd">' +
'	<ows:Identifier>Buffer</ows:Identifier>' +
'	<wps:DataInputs>' +
'		<wps:Input>' +
'			<ows:Identifier>InputPolygon</ows:Identifier>' +
'			<ows:Title>Playground area</ows:Title>' +
'			<wps:Reference xlink:href="http://foo.bar/some_WFS_request.xml" method="POST" mimeType="text/xml" encoding="UTF-8" schema="http://foo.bar/gml_polygon_schema.xsd"/>' +
'		</wps:Input>' +
'		<wps:Input>' +
'			<ows:Identifier>BufferDistance</ows:Identifier>' +
'			<ows:Title>Distance which people will walk to get to a playground.</ows:Title>' +
'			<wps:Data>' +
'				<wps:LiteralData uom="feet">400</wps:LiteralData>' +
'			</wps:Data>' +
'		</wps:Input>' +
'	</wps:DataInputs>' +
'	<wps:ResponseForm>' +
'		<wps:ResponseDocument storeExecuteResponse="true" lineage="true" status="true">' +
'			<wps:Output asReference="true">' +
'				<ows:Identifier>BufferedPolygon</ows:Identifier>' +
'				<ows:Title>Area serviced by playground.</ows:Title>' +
'				<ows:Abstract>Area within which most users of this playground will live.</ows:Abstract>' +
'			</wps:Output>' +
'			<wps:Output>' +
'				<ows:Identifier>literal</ows:Identifier>' +
'                               <ows:Title/>' +
'                               <ows:Abstract/>' +
'			</wps:Output>' +
'		</wps:ResponseDocument>' +
'	</wps:ResponseForm>' +
'</wps:Execute>';

        var format = new OpenLayers.Format.WPSExecute();
        var result = format.write({
            identifier: "Buffer",
            dataInputs: [{
                identifier: 'InputPolygon',
                title: 'Playground area',
                reference: {
                    href: 'http://foo.bar/some_WFS_request.xml',
                    method: "POST",
                    mimeType: "text/xml",
                    encoding: "UTF-8",
                    schema: "http://foo.bar/gml_polygon_schema.xsd"
                }
            }, {
                identifier: 'BufferDistance',
                title: 'Distance which people will walk to get to a playground.',
                data: {
                    literalData: {
                        value: 400,
                        uom: 'feet'
                    }
                }
            }],
            responseForm: {
                responseDocument: {
                    storeExecuteResponse: true,
                    lineage: true,
                    status: true,
                    outputs: [
                        {
                            asReference: true,
                            identifier: 'BufferedPolygon',
                            title: 'Area serviced by playground.',
                            'abstract': 'Area within which most users of this playground will live.'
                        },
                        {
                            identifier: 'literal'
                        }
                    ]
                }
            }
        });
        t.xml_eq(result, expected, "WPS Execute written out correctly");
    }

        function test_write_request_complexData(t) {
            t.plan(1);
            // taken from http://geoprocessing.info/schemas/wps/1.0/examples/51_wpsExecute_request_ResponseDocument.xml
            var expected = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' +
    '<wps:Execute service="WPS" version="1.0.0" xmlns:wps="http://www.opengis.net/wps/1.0.0" xmlns:ows="http://www.opengis.net/ows/1.1" ' +
    'xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/wps/1.0.0 http://schemas.opengis.net/wps/1.0.0/wpsAll.xsd">' +
    '	<ows:Identifier>Buffer</ows:Identifier>' +
    '	<wps:DataInputs>' +
    '		<wps:Input>' +
    '			<ows:Identifier>InputPolygon</ows:Identifier>' +
    '			<ows:Title>Playground area</ows:Title>' +
    '			<wps:Reference xlink:href="http://foo.bar/some_WFS_request.xml"/>' +
    '		</wps:Input>' +
    '		<wps:Input>' +
    '			<ows:Identifier>ResultPage</ows:Identifier>' +
    '			<ows:Title>Nicely formatted HTML of the result</ows:Title>' +
    '			<wps:Data>' +
    '				<wps:ComplexData><![CDATA[<html><head></head><body></body></head>]]></wps:ComplexData>' +
    '			</wps:Data>' +
    '		</wps:Input>' +
    '           <wps:Input>' +
    '                   <ows:Identifier>GMLPoint</ows:Identifier>' +
    '                   <ows:Title>Point as GML</ows:Title>' +
    '                   <wps:Data>' +
    '                           <wps:ComplexData><feature:geometry xmlns:feature="http://www.opengis.net/gml"><feature:Point><feature:pos>10 10</feature:pos></feature:Point></feature:geometry></wps:ComplexData>' +
    '                   </wps:Data>' +
    '           </wps:Input>' +
    '	</wps:DataInputs>' +
    '	<wps:ResponseForm>' +
    '		<wps:ResponseDocument storeExecuteResponse="true">' +
    '			<wps:Output asReference="true">' +
    '				<ows:Identifier>BufferedPolygon</ows:Identifier>' +
    '				<ows:Title>Area serviced by playground.</ows:Title>' +
    '				<ows:Abstract>Area within which most users of this playground will live.</ows:Abstract>' +
    '			</wps:Output>' +
    '		</wps:ResponseDocument>' +
    '	</wps:ResponseForm>' +
    '</wps:Execute>';

            var format = new OpenLayers.Format.WPSExecute();
            var result = format.write({
                identifier: "Buffer", 
                dataInputs: [{
                    identifier: 'InputPolygon',
                    title: 'Playground area',
                    reference: {
                        href: 'http://foo.bar/some_WFS_request.xml'
                    }
                }, {
                    identifier: 'ResultPage',
                    title: 'Nicely formatted HTML of the result',
                    data: {
                        complexData: {
                            value: "<html><head></head><body></body></head>"
                        }
                    }                    
                }, {
                    identifier: "GMLPoint",
                    title: "Point as GML",
                    data: {
                        complexData: {
                            value: OpenLayers.Format.GML.v3.prototype.writers.feature["_geometry"].apply(new OpenLayers.Format.GML.v3({curve: true, surface: true}), [new OpenLayers.Geometry.Point(10, 10)])
                        }
                    }
                }],
                responseForm: {
                    responseDocument: {
                        storeExecuteResponse: true,
                        outputs: [{
                            asReference: true,
                            identifier: 'BufferedPolygon',
                            title: 'Area serviced by playground.',
                            'abstract': 'Area within which most users of this playground will live.'
                        }]
                    }
                }
            });
            t.xml_eq(result, expected, "WPS Execute written out correctly");
        }

        function test_write_WPSExecuteFID(t) {
            t.plan(1);
          
            var result,
            expected,
            format = ({geometryName: 'the_geom'});                    
          
            expected = '<?xml version="1.0" encoding="UTF-8"?>' +
                '<wps:Execute xmlns:wps="http://www.opengis.net/wps/1.0.0" version="1.0.0" service="WPS" xsi:schemaLocation="http://www.opengis.net/wps/1.0.0 http://schemas.opengis.net/wps/1.0.0/wpsAll.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">' +
                '  <ows:Identifier xmlns:ows="http://www.opengis.net/ows/1.1">gs:Bounds</ows:Identifier>' +
                '  <wps:DataInputs>' +
                '    <wps:Input>' +
                '      <ows:Identifier xmlns:ows="http://www.opengis.net/ows/1.1">features</ows:Identifier>' +
                '      <wps:Reference mimeType="text/xml" xlink:href="http://geoserver/wfs" xmlns:xlink="http://www.w3.org/1999/xlink" method="POST">' +
                '        <wps:Body>' +
                '          <wfs:GetFeature xmlns:wfs="http://www.opengis.net/wfs" service="WFS" version="1.0.0">' +
                '            <wfs:Query typeName="foo:bar">' +
                '              <ogc:Filter xmlns:ogc="http://www.opengis.net/ogc">' +
                '                <ogc:FeatureId fid="123"/>' +
                '              </ogc:Filter>' +
                '            </wfs:Query>' +
                '          </wfs:GetFeature>' +
                '        </wps:Body>' +
                '      </wps:Reference>' +
                '    </wps:Input>' +
                '  </wps:DataInputs>' +
                '  <wps:ResponseForm>' +
                '    <wps:RawDataOutput>' +
                '      <ows:Identifier xmlns:ows="http://www.opengis.net/ows/1.1">bounds</ows:Identifier>' +
                '    </wps:RawDataOutput>' +
                '  </wps:ResponseForm>' +
                '</wps:Execute>';

            result = new OpenLayers.Format.WPSExecute().write({
                identifier: 'gs:Bounds',
                dataInputs: [{
                    identifier: 'features',
                    reference: {
                        mimeType: 'text/xml',
                        href: 'http://geoserver/wfs',
                        method: 'POST',
                        body: {
                            wfs: {
                                featureType: 'foo:bar',
                                version: '1.0.0',                            
                                filter: new OpenLayers.Filter.FeatureId({fids: [123]})
                            }
                        }
                    }
                }],
                responseForm: {
                    rawDataOutput: {
                        identifier: 'bounds'
                    }
                }
            });
            t.xml_eq(result, expected, 'WPS Execute written out correctly with a FID filter');
        }

    </script>
</head>
<body>
</body>
</html>
